﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using AstekServis.Web.Data;
using AstekServis.Web;
using System.Linq;

namespace AstekServis.Views
{
    public partial class Login : Page
    {
        AsTekDomainContext context = new AsTekDomainContext();

        public Login()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Login_Loaded);
            txtSifre.KeyDown += new System.Windows.Input.KeyEventHandler(txtSifre_KeyDown);
            txtKullaniciAdi.KeyDown += new System.Windows.Input.KeyEventHandler(txtKullaniciAdi_KeyDown);
        }

        void txtKullaniciAdi_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
                txtSifre.Focus();
        }

        void txtSifre_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                CheckLogin();
            }
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            txtKullaniciAdi.Focus();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            CheckLogin();
        }

        private void CheckLogin()
        {
            EntityQuery<Kullanici> qry = context.GetKullanicilarQuery();
            qry = qry.Where(p => p.KullaniciAdi == txtKullaniciAdi.Text);
            LoadOperation<Kullanici> operation = context.Load(qry);
            busyIndic.Visibility = System.Windows.Visibility.Visible;
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Kullanici> op = ss as LoadOperation<Kullanici>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    string sesionID = new Random().Next(10000000, 99999999).ToString("00000000");
                    Kullanici kullanici = (Kullanici)op.Entities.FirstOrDefault();
                    if (kullanici != null)
                    {
                        if (kullanici.Sifre == txtSifre.Password)
                        {
                            kullanici.SessionID = sesionID;
                            context.SubmitChanges();
                            Session.Instance.Add("user", kullanici);
                            this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
                        }
                        else
                        {
                            MessageBox.Show("Hatalı şifre !");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Kullanıcı Bulunamadı !");
                    }
                }
                busyIndic.Visibility = System.Windows.Visibility.Collapsed;
            };
        }
    }
}
